package com.jt.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jt.admin.common.utils.PageUtils;
import com.jt.admin.common.utils.Query;

import com.jt.admin.domain.mapper.SysRoleMapper;
import com.jt.admin.domain.entity.SysRoleEntity;
import com.jt.admin.service.SysRoleService;


@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleEntity> page = this.page(
                new Query<SysRoleEntity>().getPage(params),
                new QueryWrapper<SysRoleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Set<String> getRoleByUserId(Long userId) {
        Set<String> roles=new HashSet<>();
        if (userId != null && 1L == userId){
            roles.add("admin");
        }else {
            List<String> listPerms = sysRoleMapper.selectRoleByUserId(userId);
            listPerms.forEach(item->{
                if (StrUtil.isNotEmpty(item)){
                    roles.add(item);
                }
            });
        }
        return roles;
    }
}